package com.tjetc.dao.impl;

import com.tjetc.dao.RoomDao;
import com.tjetc.domain.Room;
import com.tjetc.utils.Db;
import com.tjetc.utils.PageInfo;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class RoomDaoImpl implements RoomDao {
    @Override
    public int getCount(String number) throws SQLException {
        return ((Long) Db.runner0().query("select count(id) from room where number like ? ",
                new ScalarHandler<>(),"%"+number+"%")).intValue();
    }

    @Override
    public List<Room> list(String number, PageInfo<Room> pageInfo) throws SQLException {
        return Db.runner0().query("select * from room where number like ? limit ?,?",
                new BeanListHandler<>(Room.class),"%"+number+"%",pageInfo.getStartRecord(),pageInfo.getPageSize());
    }

    @Override
    public Room findByNumber(String number) throws SQLException {
        return Db.runner0().query("select * from room where number = ?",
                new BeanHandler<>(Room.class),number);
    }

    @Override
    public int add(Room room) throws SQLException {
        return Db.runner0().update("insert into room(number,type,position,depid) values(?,?,?,?)",
                room.getNumber(),room.getType(),room.getPosition(),room.getDepid());
    }

    @Override
    public int update(Room room) throws SQLException {
        return Db.runner0().update("update room set number=?,type=?,position=?,status=?,depid=? where id=?",
                room.getNumber(),room.getType(),room.getPosition(),room.getStatus(),room.getDepid(),room.getId());
    }

    @Override
    public int del(int id) throws SQLException {
        return Db.runner0().update("delete from room where id=?",id);
    }

    @Override
    public List<Room> list1() throws SQLException {
        return Db.runner0().query("select * from room ",new BeanListHandler<>(Room.class));
    }

    @Override
    public Room findById(int roomid) throws SQLException {
        return Db.runner0().query("select * from room where id=?",new BeanHandler<>(Room.class),roomid);
    }
}
